package com.dk.controller;

import com.dk.pojo.TabSeller;
import com.dk.pojo.TabUser;
import com.dk.service.CustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @RequestMapping("getInfo")
    @ResponseBody
    public Map<String,Object> getInfo(TabUser tabUser,
                                 @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize) {
        PageInfo<TabUser> pageInfo = customerService.getInfo(tabUser,pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());//总条数
        map.put("rows", pageInfo.getList());//数据
        return map;
    }

    @RequestMapping("toAdd")
    public String toAdd() {
        return "Customer/add";
    }

    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(TabUser user) {
        int i = customerService.insertInfo(user);
        return i > 0;
    }

    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        TabUser user = customerService.getInfoById(id);
        model.addAttribute("user", user);
        return "Customer/update";
    }

    @RequestMapping("deleteInfoByUid/{uid}")
    @ResponseBody
    public boolean deleteInfoByUid(@PathVariable("uid")Integer uid){
        int i = customerService.deleteInfoByUid(uid);
        return i>0;
    }
    @RequestMapping("thaw")
    @ResponseBody
    public boolean thaw(Integer uid){
        TabUser tabUser = customerService.getInfoById(uid);
        tabUser.setFlag(0);
        int i = customerService.updateCustomer(tabUser);
        return i>0;
    }
    @RequestMapping("freeze")
    @ResponseBody
    public boolean freeze(Integer uid){
        TabUser tabUser = customerService.getInfoById(uid);
        tabUser.setFlag(3);
        int i = customerService.updateCustomer(tabUser);
        return i>0;
    }
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public boolean updateUserInfo(TabUser tabUser){
        int i = customerService.updateCustomer(tabUser);
        return i>0;
    }
}
